package com.longxi.tianjintianbao.ui.presenter.assetscheck;

import android.content.Intent;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.task.Task;
import com.longxi.tianjintianbao.ui.activity.assetscheck.AssetsContentActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.assetscheck.AssetsViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.assetscheck.IAssetsAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AssetsAtPresenter extends BasePresenter<IAssetsAtView> {

    private MultiTypeAdapter adapter;
    private Items items;

    public AssetsAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(Task.class, new AssetsViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setBar() {
        mContext.setTitle("资产盘点");
        mContext.setTvLeft(MyUtils.getString(R.string.home));
    }

    /**
     * 测试数据
     */
    public void getTaskList(String state, int page){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("state", state);
            object.put("type", "0");
            object.put("page", page);
            object.put("rowcount", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getTaskList(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<Task>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<Task> result) {
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mContext);
            }
        });
    }

    AdapterListener listener = new AdapterListener() {

        @Override
        public void onClick(int position) {
            Intent intent = new Intent(mContext, AssetsContentActivity.class);
            mContext.startActivity(intent);
        }

        @Override
        public void onDelete(int position) {

        }
    };

    public void onDestroy() {
        listener = null;
    }
}
